/*
ont-space - The ontology-based resource metadata repository
Copyright (c) 2006-2008, Information Eng. Research Unit - Univ. of Alcalá
http://www.cc.uah.es/ie
This library is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by the Free
Software Foundation; either version 2.1 of the License, or (at your option)
any later version.
This library is distributed in the hope that it will be useful, but WITHOUT
ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
details.
You should have received a copy of the GNU Lesser General Public License along
with this library; if not, write to the Free Software Foundation, Inc.,
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.ontspace.util.translator.sld2owl.owl;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import com.hp.hpl.jena.ontology.*;
import com.hp.hpl.jena.rdf.model.RDFNode;
import com.hp.hpl.jena.datatypes.RDFDatatype;
import com.hp.hpl.jena.datatypes.TypeMapper;

public class ActivityDescriptionTranslator {

    /**
     * Creates a new instance of ActivityDescriptionTranslator
     * @param lnode The activity description node of the Dom structure
     * @param ont The ontology
     * @param instActivity The activity instance
     * @param nsName The namespace
     */
    public ActivityDescriptionTranslator(Node lnode, OntModel ont,
            Individual instActivity, String nsName) {

        lastInstanceSLDTranslator lastInst =
                new lastInstanceSLDTranslator();

        try {

            OntClass actDes =
                    ont.getOntClass(nsName + "Activity-Description");
            ObjectProperty activityDescription =
                    ont.getObjectProperty(nsName + "activity-description");

            DatatypeProperty identifier =
                    ont.getDatatypeProperty(nsName + "identifier");
            DatatypeProperty identifierRef =
                    ont.getDatatypeProperty(nsName + "identifier-ref");
            DatatypeProperty title =
                    ont.getDatatypeProperty(nsName + "title");
            DatatypeProperty isVisible =
                    ont.getDatatypeProperty(nsName + "isvisible");
            ObjectProperty metadataRef =
                    ont.getObjectProperty(nsName + "metadata-ref");

            //Create the instance and add the relation attribute to 
            //the activity description instance which link whith this
            String instance =
                    "activity_description_instance_" +
                    (String.valueOf(lastInst.lastInstance(actDes)));
            Individual instLd =
                    ont.createIndividual(nsName + instance, actDes);
            instActivity.setPropertyValue(activityDescription, instLd);


            NodeList children =
                    lnode.getChildNodes();
            String ldTitle =
                    "";
            if (children != null) {
                for (int i =
                        0; i < children.getLength(); i++) {
                    if (children.item(i).getNodeName().compareTo("imsld:title") ==
                            0) {
                        ldTitle =
                                children.item(i).getTextContent();
                        if (!(ldTitle.compareTo(" ") == 0)) {
                            RDFNode titleValue =
                                    ont.createTypedLiteral(ldTitle);
                            instLd.setPropertyValue(title, titleValue);
                        }
                    }
                    if (children.item(i).getNodeName().compareTo("imsld:item") ==
                            0) {
                        Node itemNode =
                                children.item(i);
                        NamedNodeMap attrsItem =
                                itemNode.getAttributes();
                        for (int x =
                                0; x < attrsItem.getLength(); x++) {
                            Node attr =
                                    attrsItem.item(x);
                            if (attr.getNodeName().trim().compareTo("identifier") ==
                                    0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#ID");
                                RDFNode identifierValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLd.setPropertyValue(identifier,
                                        identifierValue);
                            }
                            if (attr.getNodeName().trim().compareTo("identifierref") ==
                                    0) {
                                RDFNode identifierRefValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim());
                                instLd.setPropertyValue(identifierRef,
                                        identifierRefValue);
                            }
                            if (attr.getNodeName().trim().
                                    compareTo("isvisible") == 0) {
                                RDFNode visibleValue;
                                if (attr.getNodeValue().trim().compareTo("true") ==
                                        0) {
                                    visibleValue =
                                            ont.createTypedLiteral(true);
                                } else {
                                    visibleValue =
                                            ont.createTypedLiteral(false);
                                }
                                instLd.setPropertyValue(isVisible, visibleValue);
                            }

                            if (attr.getNodeName().trim().
                                    compareTo("metadataLOMref") == 0) {
                                TypeMapper tm =
                                        TypeMapper.getInstance();
                                RDFDatatype overType =
                                        tm.getSafeTypeByName("http://www.w3.org/2001/XMLSchema#Metadata");
                                RDFNode metadataRefValue =
                                        ont.createTypedLiteral(attr.getNodeValue().
                                        trim(), overType);
                                instLd.setPropertyValue(metadataRef,
                                        metadataRefValue);
                            }

                        }   //for   
                    }//if           

                }//for                  
            }//if
        }//try
        catch (Exception ex) {
            ex.printStackTrace();

        }
    }
    }
